package fun.ticsmyc.tools;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author Ticsmyc
 * @package fun.ticsmyc.tools
 * @date 2020-02-05 15:45
 */
public class 比较器 {

    private Object IdAscendingComparator;

    class Student{
        public String name;
        public int id;
        public int age;

        public Student(String name, int id, int age) {
            this.name = name;
            this.id = id;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    ", age=" + age +
                    '}';
        }
    }

    /**
     * 比较器：
         正数升序，负数降序。
     */
    public static class IdAscendingComparator implements Comparator<Student>{
        /**
         * 返回负数： 第一个参数放前面
         * 返回正数：第二个参数放前面
         * 返回0 ： 两个参数一样
         * @param o1
         * @param o2
         * @return
         */
        @Override
        public int compare(Student o1, Student o2) {
//            if(o1.id < o2.id){
//                //让o1排在o2前面， 所以返回负数
//                return -1;
//            }else if(o1.id>o2.id){
//                return 1;
//            }else{
//                return 0;
//            }
            return o1.id-o2.id;
        }
    }

    private void print(Student[] students){
        for(Student s :students){
            System.out.println(s);
        }

    }

    @Test
    public void test(){
        Student s1=  new Student("AAA",1,3);
        Student s2  =  new Student("BBB",2,3);
        Student s3 = new Student ("CCC",3,3);
        Student[] students=new Student[]{s3,s1,s2};
        print(students);
        Arrays.sort(students,new IdAscendingComparator());
        print(students);
    }
}
